| Module | SimplyPresentable::Renderer |
| In: |
vendor/plugins/simply_presentable/lib/simply_presentable/renderer.rb
|
present locates the appropriate presenters for an object, instantiates a presenter proxy and then exposes the presenters public methods through the object. The optional_presenter_names parameter allows the declaration of additional presenters that should be instantiated and exposed. The names should be in the same format as the class names in Presenter::ClassMethods.used_for,
If the presentable is already a presenter, it will be left unchanged WITH THE EXCEPTION OF THE RENDERER, which will be changed to the receiver (this object).
# File vendor/plugins/simply_presentable/lib/simply_presentable/renderer.rb, line 10
10: def present(presentable, *optional_presenter_underscored_class_names)
11: if presentable.respond_to?(:presenter_proxy)
12: returning presentable do
13: presentable.renderer = self
14: end
15: else
16: PresenterProxy.new(self, presentable, optional_presenter_underscored_class_names)
17: end
18: end
with_presenters_for yields a PresenterProxy for each presentable object. If the last
presentable object is a Hash, then the presentable is taken to be the key and the value is expected to
be an array of optional underscored presenter class names (see present). As such, this method will not yield presenters for hashes as the last argument.
If the presentables are already presenters, the renderer will be changed to the caller for the duration of the block
Then renderer will be set to this object, but then will be set back to its previous value. You can nest with_presenters_for calls and the original renderer will still be rolled back.
Examples:
with_presenters_for(foo, bar) do |foo_presenter, bar_presenter|
foo_presenter.some_default_presentation_method
bar_presenter.some_default_presentation_method
end
with_presenters_for(foo, bar => :baz, baw => [:bam, :blip]) do |foo_presenter, bar_presenter|
foo_presenter.some_default_presentation_method
bar_presenter.some_baz_presentation_method
baw_presenter.some_bam_presentation_method
baw_presenter.some_blip_presentation_method
end
#in controller:
present(foo).render
#in partial
<% with_presenters_for foo do |foo_presenter| %>
<% foo.div_tag do %> # This wouldn't work if the renderer weren't changed since the controller doesn't respond to content_tag
<%= foo.render(:partial => 'sub_foo') %>
<% end %>
<% end %>
# File vendor/plugins/simply_presentable/lib/simply_presentable/renderer.rb, line 53
53: def with_presenters_for(*presentables)
54: renderer = self
55: hash_presentable = presentables.last.is_a?(Hash) ? presentables.pop : nil
56: presentables.each { |p| p.cache_renderers if p.respond_to?(:presenter_proxy) }
57: presenter_proxies = presentables.map { |p| renderer.present(p) } + hash_presentable.to_a.map { |k, v| renderer.present(k, *[v].flatten) }
58: returning yield(*presenter_proxies) do
59: presentables.each { |p| p.restore_renderers if p.respond_to?(:presenter_proxy) }
60: end
61: end